package lynch.manager
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import Elegant.utils.core.Singleton;
	import Elegant.utils.InitObject;
	import Elegant.utils.VectorUtil;
	import flash.display.Sprite;
	import lynch.base.ModuleBase;
	import lynch.core.Core;
	import lynch.net.loading.ResourceLoader;
	
	/**
	 * 界面管理,控制模块的显示与否
	 */
	public final class UIManager
	{
		/**
		 * @private
		 */
		arcane var mouseLayer:Sprite;
		/**
		 * @private
		 */
		arcane var alertLayer:Sprite;
		/**
		 * for modules
		 */
		public var topLayer:Sprite;
		
		/**
		 * for players npcs
		 */
		public var middleLayer:Sprite;
		
		/**
		 * for map
		 */
		public var buttomLayer:Sprite;
		
		private static var _rootSprite:Sprite = new Sprite();
		private static var _currentModule:Vector.<ModuleBase>;
		
		public function UIManager()
		{
			arcane::mouseLayer = new Sprite();
			arcane::alertLayer = new Sprite();
			topLayer = new Sprite();
			middleLayer = new Sprite();
			buttomLayer = new Sprite();
			
			_rootSprite.addChild(buttomLayer);
			_rootSprite.addChild(middleLayer);
			_rootSprite.addChild(topLayer);
			_rootSprite.addChild(arcane::alertLayer);
			_rootSprite.addChild(arcane::mouseLayer);
		}
		
		public static function get getInstance():UIManager
		{
			return Singleton.getInstance(UIManager);
		}
		
		public static function centerModule(target:ModuleBase):void
		{
			target.x = (Core.config.stageWidth - (target.moduleWidth >= 0 ? target.moduleWidth : target.width)) >> 1;
			target.y = (Core.config.stageHeight - (target.moduleHeight >= 0 ? target.moduleHeight : target.height)) >> 1;
		}
		
		/**
		 * 打开模块
		 * @param	name
		 * @param	data	该data可以在各个模块的主类中直接使用,如果ExampleMain.data;
		 * @param	objectVars		
		 */
		public function openModule(name:String, data:* = null, objectVars:Object = null):void
		{
			var module:ModuleBase = ResourceLoader.getCacheDatas(name)[0];
			
			if (module == null)
			{
				//由ModuleCore.config.置
				var moduleItem:XML = Core.config.configXML.moduleConfig.subModule.(@name == name);
				var res:XMLList = moduleItem.resource;
				var l:int = res.length();
				var urls:Vector.<String> = Vector.<String>(l + 1);
				for (var i:int = 0; i < l; ++i) 
				{
					urls[i] = res[i].@src;
				}
				urls.unshift(moduleItem.@src);
				ResourceLoader.loadSWFs(urls, onModuleLoaded, { name: moduleItem.@name, params: [data] } );
			}
			else
			{
				onModuleLoaded([module], data);
			}
		}
		
		/**
		 * 关闭模块
		 * @param	name	module name
		 */
		public function closeModule(name:String):void
		{
			var i:int = VectorUtil.indexInVec(Vector.<Object>(_currentModule), name, "name");
			
			if (i != -1)
			{
				_currentModule[i].close();
				//no _currentModule.splice(i, 1)
				//because of when a user close a module from module.close(), it will not do splice
			}
		}
		
		/**
		 * @private
		 * @param	mdb
		 */
		arcane function closeFromModule(mdb:ModuleBase):void 
		{
			topLayer.removeChild(mdb);
			_currentModule.splice(_currentModule.indexOf(mdb), 1);
		}
		
		//--------------------------------------------------
		private function onModuleLoaded(arr:Array, data:*):void
		{
			var module:ModuleBase = arr[0];
			
			var compatibleModule:Vector.<String> = module.compatibleModule;
			for each (var item:ModuleBase in _currentModule) 
			{
				if(compatibleModule.indexOf(item.name) == -1)
					item.close();
			}
				
			module.data = data;
			
			centerModule(module);
			topLayer.addChild(module);
			
			module.open();
			_currentModule.push(module);
		}
		
		//-------------------getter / setter-----------------
		
		/**
		 *
		 */
		public static function get currentModule():Vector.<ModuleBase>
		{
			return _currentModule;
		}
		
		/**
		 *
		 */
		public static function get rootSprite():Sprite
		{
			return _rootSprite;
		}
	}
}